package com.train.trainmanage.serviceImpl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.train.trainmanage.mapper.ComeGoDirectionModelMapper;
import com.train.trainmanage.mapper.DepartmentModelMapper;
import com.train.trainmanage.mapper.LocomotiveModelMapper;
import com.train.trainmanage.mapper.StationModelMapper;
import com.train.trainmanage.mapper.TrainLocoRelationModelMapper;
import com.train.trainmanage.mapper.TrainModelMapper;
import com.train.trainmanage.model.ComeGoDirectionModel;
import com.train.trainmanage.model.DepartmentModel;
import com.train.trainmanage.model.LocomotiveModel;
import com.train.trainmanage.model.StationModel;
import com.train.trainmanage.model.TrainModel;
import com.train.trainmanage.service.TrainService;

@Service
public class TrainServiceImpl implements TrainService {

	@Autowired
	private TrainModelMapper trainMapper;
	@Autowired
	private LocomotiveModelMapper locoMapper;
	@Autowired
	private StationModelMapper stationMapper;
	@Autowired
	private ComeGoDirectionModelMapper comeGoDirecMapper;
	@Autowired
	private DepartmentModelMapper departMapper;
	@Autowired
	private TrainLocoRelationModelMapper trainLocoRelaMapper;
	
	@Override
	public List<TrainModel> listByTrainModel(TrainModel train) {
		Map<String,Object> map = new HashMap<String,Object>(); 
		if(train != null) {
			if(train.getTrainName() != null) {
				map.put("trainName", train.getTrainName());
			}
			if(train.getStartStationId() != null) {
				map.put("startStationId", train.getStartStationId());
			}
			if(train.getEndStationId() != null) {
				map.put("endStationId", train.getEndStationId());
			}
			if(train.getIsAddWater() != null) {
				map.put("isAddWater", train.getIsAddWater());
			}
			if(train.getIsDealRubbish() != null) {
				map.put("isDealRubbish", train.getIsDealRubbish());
			}
			if(train.getPageNo() != null && train.getPageNo() > 1) {
				map.put("beginNum", (train.getPageNo() - 1) * train.getPageSize());
			}else {
				map.put("beginNum", 0);
			}
			
			map.put("pageSize", train.getPageSize());
			
		}
		
		List<TrainModel> result = trainMapper.listByMap(map);
		if(result != null && result.size() >0) {
			for(TrainModel temTrain : result) {
				Integer startStationId = temTrain.getStartStationId();
				StationModel startStation = stationMapper.selectByPrimaryKey(startStationId);
				if(startStation != null && startStation.getStationName() != null) {
					temTrain.setStartStationName(startStation.getStationName());
				}
				Integer endStationId = temTrain.getEndStationId();
				StationModel endStation = stationMapper.selectByPrimaryKey(endStationId);
				if(endStation != null && endStation.getStationName() != null) {
					temTrain.setEndStationName(endStation.getStationName());
				}
				
			}
		}
		
		return result;
	}
	
	
	@Override
	public TrainModel selectById(Integer trainId) {
		return trainMapper.selectByPrimaryKey(trainId);
	}
	
	@Override
	public int getCountByTrain(TrainModel train) {
		Map<String,Object> map = new HashMap<String,Object>(); 
		if(train != null) {
			if(train.getTrainName() != null) {
				map.put("trainName", train.getTrainName());
			}
			if(train.getStartStationId() != null) {
				map.put("startStationId", train.getStartStationId());
			}
			if(train.getEndStationId() != null) {
				map.put("endStationId", train.getEndStationId());
			}
			if(train.getIsAddWater() != null) {
				map.put("isAddWater", train.getIsAddWater());
			}
			if(train.getIsDealRubbish() != null) {
				map.put("isDealRubbish", train.getIsDealRubbish());
			}
		}
		return trainMapper.getCountByMap(map);
	}
	@Override
	public TrainModel getInfoById(Integer trainId) {
		TrainModel train = trainMapper.selectByPrimaryKey(trainId);
		if(train != null) {
			LocomotiveModel locomotive = locoMapper.selectByTrainId(trainId);
			if(locomotive != null) {
				DepartmentModel department = departMapper.selectByPrimaryKey(locomotive.getBearBureau());
				locomotive.setBearBureauName(department.getDepartName());
				train.setLocomotive(locomotive);
			}
			StationModel startStation = stationMapper.selectByPrimaryKey(train.getStartStationId());
			if(startStation != null) {
				train.setStartStationName(startStation.getStationName());
			}
			StationModel afterStation = stationMapper.selectByPrimaryKey(train.getAfterStationId());
			if(afterStation != null) {
				train.setAfterStationName(afterStation.getStationName());
			}
			StationModel frontStation = stationMapper.selectByPrimaryKey(train.getFrontStationId());
			if(frontStation != null) {
				train.setFrontStationName(frontStation.getStationName());
			}
			StationModel endStation = stationMapper.selectByPrimaryKey(train.getEndStationId());
			if(endStation != null) {
				train.setEndStationName(endStation.getStationName());
			}
			
			ComeGoDirectionModel comeDirec = comeGoDirecMapper.selectByPrimaryKey(train.getComeDirectionId());
			if(comeDirec != null) {
				train.setComeDirectionName(comeDirec.getDirecName());
			}
			ComeGoDirectionModel goDirec = comeGoDirecMapper.selectByPrimaryKey(train.getGoDirectionId());
			if(goDirec != null) {
				train.setGoDirectionName(goDirec.getDirecName());
			}
		}
		
		return train;
	}

	@Override
	public int addTrain(TrainModel train) {
		return trainMapper.insertSelective(train);
	}

	@Override
	public int editTrain(TrainModel train) {
		return trainMapper.updateByPrimaryKeySelective(train);
	}

	@Override
	public TrainModel getByTrainName(String trainName) {
		TrainModel train = trainMapper.selectByTrainName(trainName);
		return train;
	}

	@Override
	public int updateTrainLocoRelaStateByJob() {
		int result = trainLocoRelaMapper.updateStateByJonb();
		return result;
	}


	@Override
	public int addTrainByExcel(List<TrainModel> trainList) {
		Integer result = 0;
		if(trainList != null && trainList.size() > 0) {
			for(TrainModel temTrain : trainList) {
				//首先判断车次是否已经存在,已经存在的不再插入
				TrainModel oldTrain = trainMapper.selectByTrainName(temTrain.getTrainName());
				if(oldTrain != null) {
					continue;
				}
				
				if(temTrain.getStartStationName() != null && !"".equals(temTrain.getStartStationName())) {
					StationModel temStation = stationMapper.selectByName(temTrain.getStartStationName());
					if(temStation != null) {
						temTrain.setStartStationId(temStation.getStationId());
					}
				}
				if(temTrain.getEndStationName() != null && !"".equals(temTrain.getEndStationName())) {
					StationModel temStation = stationMapper.selectByName(temTrain.getEndStationName());
					if(temStation != null) {
						temTrain.setEndStationId(temStation.getStationId());
					}
				}
				/**根据车次数的奇偶性，判断车次是上行/下行,以此来修改车次的前方站，后方站信息 
				 * 奇数 是 下行;偶数是上行
				 * 前方站：车次要去的方向,也就是下一站
				 * 后方站：车次来车的方向,也就是上一站
				 */
				String trainName = temTrain.getTrainName();
				Integer theLastNoOfName = 0;
				String theLastNoOfNameStr = "";
				if(trainName.contains(")")) {
					theLastNoOfNameStr = trainName.substring(trainName.length()-2, trainName.length()-1);
				}else {
					theLastNoOfNameStr = trainName.substring(trainName.length()-1, trainName.length());
				}
				if(theLastNoOfNameStr != "") {
					final String number = "0123456789";
					if(number.contains(theLastNoOfNameStr)) {
						theLastNoOfName = Integer.valueOf(theLastNoOfNameStr);
					}
				}
				
				if(theLastNoOfName%2 == 0) {//偶数    --->  上行
					Date oldReachTime = temTrain.getReachTime();
					Date oldLeaveTime = temTrain.getLeaveTime();
					temTrain.setReachTime(oldLeaveTime);
					temTrain.setLeaveTime(oldReachTime);
					if(temTrain.getFrontStationName() != null && !"".equals(temTrain.getFrontStationName())) {
						StationModel temStation = stationMapper.selectByName(temTrain.getFrontStationName());
						if(temStation != null) {
							temTrain.setFrontStationId(temStation.getStationId());
						}
						String temTrainName = "";
						if(temTrain.getFrontStationName().contains("疏解区")) {
							temTrainName = "疏解区线路所发车";
						}else if(temTrain.getFrontStationName().contains("曹古寺")) {
							temTrainName = "曹古寺线路所发车";
						}else if(temTrain.getFrontStationName().contains("二郎庙")) {
							temTrainName = "二郎庙线路所发车";
						}else if(temTrain.getFrontStationName().contains("马头岗")) {
							temTrainName = "马头岗线路所发车";
						}else if(temTrain.getFrontStationName().contains("鸿宝线")) {
							temTrainName = "鸿宝线路所发车";
						}else if(temTrain.getDepartureTrack() >= 1 || temTrain.getDepartureTrack() <= 9) {
							temTrainName = "动车走行一线发车";
						}else if(temTrain.getDepartureTrack() >= 12 || temTrain.getDepartureTrack() <= 16) {
							temTrainName = "动车走行二线发车";
						}
						
						ComeGoDirectionModel goDirection = comeGoDirecMapper.getGoDirecByTrainname(temTrainName);
						if(goDirection != null) {
							temTrain.setGoDirectionId(goDirection.getDirecId());
							String serialNumberStr = goDirection.getSerialNumber();
							String goTrackNo = temTrain.getDepartureTrack().toString();
							serialNumberStr = serialNumberStr.replaceFirst("\\?", goTrackNo);
							temTrain.setDepartureSequence(serialNumberStr);
						}
						
					}
					if(temTrain.getAfterStationName() != null && !"".equals(temTrain.getAfterStationName())) {
						StationModel temStation = stationMapper.selectByName(temTrain.getAfterStationName());
						if(temStation != null) {
							temTrain.setAfterStationId(temStation.getStationId());
						}
						String temTrainName = "";
						if(temTrain.getAfterStationName().contains("疏解区")) {
							temTrainName = "疏解区线路所来车";
						}else if(temTrain.getAfterStationName().contains("曹古寺")) {
							temTrainName = "曹古寺线路所来车";
						}else if(temTrain.getAfterStationName().contains("二郎庙")) {
							temTrainName = "二郎庙线路所来车";
						}else if(temTrain.getAfterStationName().contains("马头岗")) {
							temTrainName = "马头岗线路所来车";
						}else if(temTrain.getAfterStationName().contains("鸿宝线")) {
							temTrainName = "鸿宝线路所来车";
						}else if(temTrain.getDepartureTrack() >= 1 || temTrain.getDepartureTrack() <= 9) {
							temTrainName = "动车走行一线来车";
						}else if(temTrain.getDepartureTrack() >= 12 || temTrain.getDepartureTrack() <= 16) {
							temTrainName = "动车走行二线来车";
						}
						
						ComeGoDirectionModel comeDirection = comeGoDirecMapper.getComeDirecByTrainname(temTrainName);
						if(comeDirection != null) {
							temTrain.setComeDirectionId(comeDirection.getDirecId());
							String serialNumberStr = comeDirection.getSerialNumber();
							String comeTrackNo = temTrain.getPickUpTrack().toString();
							serialNumberStr = serialNumberStr.replaceFirst("\\?", comeTrackNo);
							temTrain.setPickUpSequence(serialNumberStr);
						}
					}
					
				}else if(theLastNoOfName%2 == 1){//奇数  下行
					if(temTrain.getFrontStationName() != null && !"".equals(temTrain.getFrontStationName())) {
						StationModel temStation = stationMapper.selectByName(temTrain.getFrontStationName());
						if(temStation != null) {
							temTrain.setAfterStationId(temStation.getStationId());
						}
						String temTrainName = "";
						if(temTrain.getFrontStationName().contains("疏解区")) {
							temTrainName = "疏解区线路所来车";
						}else if(temTrain.getFrontStationName().contains("曹古寺")) {
							temTrainName = "曹古寺线路所来车";
						}else if(temTrain.getFrontStationName().contains("二郎庙")) {
							temTrainName = "二郎庙线路所来车";
						}else if(temTrain.getFrontStationName().contains("马头岗")) {
							temTrainName = "马头岗线路所来车";
						}else if(temTrain.getFrontStationName().contains("鸿宝线")) {
							temTrainName = "鸿宝线路所来车";
						}else if(temTrain.getDepartureTrack() >= 1 || temTrain.getDepartureTrack() <= 9) {
							temTrainName = "动车走行一线来车";
						}else if(temTrain.getDepartureTrack() >= 12 || temTrain.getDepartureTrack() <= 16) {
							temTrainName = "动车走行二线来车";
						}
						ComeGoDirectionModel comeDirection = comeGoDirecMapper.getComeDirecByTrainname(temTrainName);
						if(comeDirection != null) {
							temTrain.setComeDirectionId(comeDirection.getDirecId());
							String serialNumberStr = comeDirection.getSerialNumber();
							String comeTrackNo = temTrain.getPickUpTrack().toString();
							serialNumberStr = serialNumberStr.replaceFirst("\\?", comeTrackNo);
							temTrain.setPickUpSequence(serialNumberStr);
						}
					}
					if(temTrain.getAfterStationName() != null && !"".equals(temTrain.getAfterStationName())) {
						StationModel temStation = stationMapper.selectByName(temTrain.getAfterStationName());
						if(temStation != null) {
							temTrain.setFrontStationId(temStation.getStationId());
						}
						String temTrainName = "";
						if(temTrain.getAfterStationName().contains("疏解区")) {
							temTrainName = "疏解区线路所发车";
						}else if(temTrain.getAfterStationName().contains("曹古寺")) {
							temTrainName = "曹古寺线路所发车";
						}else if(temTrain.getAfterStationName().contains("二郎庙")) {
							temTrainName = "二郎庙线路所发车";
						}else if(temTrain.getAfterStationName().contains("马头岗")) {
							temTrainName = "马头岗线路所发车";
						}else if(temTrain.getAfterStationName().contains("鸿宝线")) {
							temTrainName = "鸿宝线路所发车";
						}else if(temTrain.getDepartureTrack() >= 1 || temTrain.getDepartureTrack() <= 9) {
							temTrainName = "动车走行一线发车";
						}else if(temTrain.getDepartureTrack() >= 12 || temTrain.getDepartureTrack() <= 16) {
							temTrainName = "动车走行二线发车";
						}
						ComeGoDirectionModel goDirection = comeGoDirecMapper.getGoDirecByTrainname(temTrainName);
						if(goDirection != null) {
							temTrain.setGoDirectionId(goDirection.getDirecId());
							String serialNumberStr = goDirection.getSerialNumber();
							String goTrackNo = temTrain.getDepartureTrack().toString();
							serialNumberStr = serialNumberStr.replaceFirst("\\?", goTrackNo);
							temTrain.setDepartureSequence(serialNumberStr);
						}
					}
					
				}
				
				result = trainMapper.insertSelective(temTrain);
			}
		}
		
		return result;
	}


	@Override
	public int deleteAll() {
		return trainMapper.deleteAll();
	}


	

	
	
	
	
	

}
